package com.qk.controller;

import com.qk.anno.Log;
import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.dto.UserDto;
import com.qk.entity.User;
import com.qk.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/users")
public class UserController {
    @Autowired
    private UserService userService;
    @GetMapping
    public Result list(UserDto userDto)
    {
        log.info("查询所有用户: {}",userDto);
        PageResult<User> pageResult = userService.listUsers(userDto);
        return Result.success(pageResult);
    }
 /*   @DeleteMapping("{id}")
    public Result delete(@PathVariable Integer id)
    {
        log.info("删除用户: {}" , id);
        userService.deleleByid(id);
        return Result.success();
    }*/
 @Log
    @PostMapping
    public Result insert(@RequestBody User user)
    {
        log.info("新增用户: {}" , user);
        userService.insert(user);
        return Result.success();
    }
    @GetMapping("{id}")
    public Result getById(@PathVariable Integer id)
    {
        log.info("根据ID查询用户: {}" , id);
        User user = userService.getById(id);
        return Result.success(user);
    }
    @Log
    @PutMapping
    public Result update(@RequestBody User user)
    {
        log.info("更新用户: {}" , user);
        userService.update(user);
        return Result.success();
    }
    @GetMapping("/list")
    public Result list()
    {
        log.info("查询所有用户");
        List<User> userList= userService.getUser();
        return Result.success(userList);
    }
    @GetMapping("/role/{roleLabel}")
    public Result listByRole(@PathVariable String roleLabel)
        {
            log.info("查询角色为{}的用户",roleLabel);
            List<User> userList= userService.listByRole(roleLabel);
            return Result.success(userList);
        }
        @GetMapping("/dept/{deptId}")
    public Result listByDept(@PathVariable Integer deptId)
        {
            log.info("查询部门为{}的用户",deptId);
            List<User> userList= userService.listByDept(deptId);
            return Result.success(userList);
        }
    @Log
        @DeleteMapping("{ids}")
    public Result deletes(@PathVariable List<Integer> ids)
        {
            log.info("批量删除用户: {}" , ids);
            userService.deletes(ids);
            return Result.success();
        }
}
